package com.bruce.springai.controller;

import java.time.LocalDate;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.bruce.springai.dto.ChatMessage;
import com.bruce.springai.tools.UserTools;

import jakarta.annotation.Resource;
import reactor.core.publisher.Flux;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;


@RestController
@RequestMapping("/ai")
public class AiController {
    
    @Resource
    private ChatClient chatClient ;

    @Resource
    private UserTools userTools ;

    @PostMapping("/findUser")
    public Flux<String> findUser(@RequestBody ChatMessage chatMessage) {
        
       Flux<String> responseContent = chatClient.prompt(chatMessage.getMessage())
       .advisors(a -> a.param(ChatMemory.CONVERSATION_ID, chatMessage.getMemoryId()))
       .system(s -> s.param("current_date", LocalDate.now().toString()))  // 给系统提示词中的占位符{current_date}赋值     
       .tools(userTools)
       .stream().content() ;

        return responseContent.concatWith(Flux.just("[complete]"));
    }
    
}
